﻿//----------------------------------------------------------------
//CodeArt.
//
//文件描述:
//
//创 建 人: dreamnan@hotmail.com(Blog:http://www.cnblogs.com/dreamnan)
//创建日期: 2009-10-15
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;

namespace CodeArt.SharePoint.Fields
{
    [CLSCompliant(false)]
    public class PickerLookupFieldControl : BaseFieldControl
    {
        private const string delimiter = ";#";

        LookupPicker _recordPicker = null;

        public override object Value
        {
            get
            {
                EnsureChildControls();

                string str = "";
                if (this.Page.IsPostBack)
                {
                    this._recordPicker.Validate();
                }

                if (this._recordPicker == null) return str;

                ArrayList resolvedEntities = this._recordPicker.ResolvedEntities;
                if (resolvedEntities.Count <= 0)
                {
                    return str;
                }

                SPPickerLookupField field = (SPPickerLookupField)Field;

                PickerEntity entity = (PickerEntity)resolvedEntities[0];
                               
                string str2 = null;
                string str3 = null;

                if (entity.EntityData.ContainsKey(LookupPickerData.LookupIDField))
                {
                    str2 = entity.EntityData[LookupPickerData.LookupIDField].ToString();
                    str3 = entity.EntityData[LookupPickerData.LookupNameField].ToString();
                }
                else
                    str2 = entity.Key + delimiter + entity.DisplayText;

                if (string.IsNullOrEmpty(str2))
                {
                    if (string.IsNullOrEmpty(str3))
                    {
                        return ("-1;#" + entity.Key);
                    }
                    return str3;
                }
                return str2;
            }
            set
            {
                EnsureChildControls();

                if (this._recordPicker != null)
                {
                    this._recordPicker.Entities.Clear();

                    SPPickerLookupField field = (SPPickerLookupField)Field;

                    SPFieldLookupValue value2 = value as SPFieldLookupValue;
                    if (value2 == null)
                    {
                        string fieldValue = value as string;
                        if (fieldValue != null)
                        {
                            value2 = new SPFieldLookupValue(fieldValue);
                        }
                    }
                    if (value2 != null)
                    {
                        PickerEntity entity = LookupPicker.CreatePickerEntity(Item, value2.LookupValue,
                            field.LookupField, _recordPicker, value2.LookupId.ToString());
                        ArrayList entities = new ArrayList();
                        entities.Add(entity);
                        _recordPicker.UpdateEntities(entities);
                    }
                }
            }
        }

        public PickerLookupFieldControl()
        {

        }

        public override void Focus()
        {
            if (!base.InDesign)
            {
                this.EnsureChildControls();
            }
        }
        
        public override void Validate()
        {
            EnsureChildControls();
            if (ControlMode != SPControlMode.Display && IsValid)
            {
                //base.Validate();
                //int num = (int)this.Value;
                _recordPicker.Validate();
                if (Field.Required && _recordPicker.Entities.Count == 0)
                {
                    IsValid = false;
                    _recordPicker.ErrorMessage = "The field is required";
                    ErrorMessage = " ";
                }
                if (_recordPicker.IsValid == false)
                {
                    IsValid = false;
                    _recordPicker.ErrorMessage = "Invalid value";
                    ErrorMessage = " ";
                }
            }
        }

        protected override void CreateChildControls()
        {
            if (!base.IsFieldValueCached)
            {
                if (base.Field == null)
                {
                    return;
                }
                base.CreateChildControls();
                if (base.ControlMode == SPControlMode.Display)
                {
                    return;
                }
                SPPickerLookupField field = (SPPickerLookupField)base.Field;
                if (field.AllowMultipleValues)
                {
                    return;
                }
                this.Controls.Clear();

                this._recordPicker = new LookupPicker();
                //_recordPicker.TabIndex = 1;
                this._recordPicker.TabIndex = this.TabIndex;
                this._recordPicker.CssClass = this.CssClass;
                //this._recordPicker.ValidatorEnabled = true;
                this._recordPicker.Rows = 1;

                try
                {
                    LookupPickerData data = new LookupPickerData( field.LookupWebId, new Guid(field.LookupList),field.LookupField);
                    _recordPicker.PickerData = data;
                }
                catch (Exception e)
                {
                    _recordPicker.ErrorMessage =
                        "An error occured: " + e.Message;
                    _recordPicker.Enabled = false;
                }
                Controls.Add(_recordPicker);
                ChildControlsCreated = true;
            }
            else
            {
                base.CreateChildControls();
                return;
            }

            //this.SetFieldControlValue(this.ItemFieldValue);

        }

        public override string CssClass
        {
            get
            {
                return "ms-usereditor";

                //if (base.CssClass == null)
                //{
                //    return "ms-usereditor";
                //}
                //return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
        }


    }
}


